<template>
	<a-modal
		v-model:visible="visible"
		title="报废"
		:mask-closable="false"
		:width="600"
		:destroy-on-close="true"
		@ok="handleOk"
		@cancel="handleCancel"
		:confirmLoading="uploadLoading"
	>
		<a-form ref="formRef" :model="formData">
			<a-form-item label="报废原因：" name="scrapReason" :rules="{ required: true, message: '请输入报废原因' }">
				<a-textarea v-model:value="formData.scrapReason" placeholder="请输入报废原因" allow-clear :rows="4"
							:disabled="dataState===0"/>
			</a-form-item>
			<a-form-item label="附件列表：" name="fileList">
				<a-upload
					v-model:file-list="fileList"
					:custom-request="customRequest"
					:show-upload-list="{showRemoveIcon: dataState!=0}"
				>
					<a-button v-if="dataState!=0 && fileList.length < 9">
						<upload-outlined/>
						上传
					</a-button>
				</a-upload>
			</a-form-item>
		</a-form>
	</a-modal>
</template>
<script setup name="bizGoodsScrap">

	import fileApi from '@/api/dev/fileApi'
	import sysConfig from '@/config/index'
	import {cloneDeep} from "lodash-es";
	import bizGoodsApi from "@/api/biz/bizGoodsApi";

	const visible = ref(false)
	const emit = defineEmits({successful: null})
	const uploadLoading = ref(false)
	const formRef = ref()
	// 表单数据
	const formData = ref({})

	const fileList = ref([])

	const dataState = ref(undefined)

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		if (record) {
			bizGoodsApi.bizGoodsScrapInfo({
				goodsId: record.id
			}).then((data) => {
				let files = []
				if (data.fileList.length > 0) {
					data.fileList.map((item) => {
						files.push({
							uid: item.uid,
							name: item.name,
							url: sysConfig.API_URL + '/dev/file/download?id=' + item.uid,
							id: item.uid
						})
					})
				}
				formData.value = Object.assign({}, data)
				fileList.value = files
				dataState.value = data.state
			})
		}
	}

	// 确定
	const handleOk = () => {
		if (dataState.value === 0) {
			handleClose()
		} else {
			formRef.value
				.validate()
				.then(() => {
					uploadLoading.value = true
					const fileIds = []
					fileList.value.map((item) => {
						console.warn("item=>", item)
						fileIds.push(item.id)
					})
					const formDataParam = {
						goodsId: formData.value.id,
						scrapReason: formData.value.scrapReason,
						fileIds: fileIds
					}
					bizGoodsApi
						.bizGoodsScrap(formDataParam)
						.then(() => {
							handleClose()
							emit('successful')
						})
						.finally(() => {
							uploadLoading.value = false
						})
				})
		}
	}

	const handleClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		fileList.value = []
		dataState.value = undefined
		visible.value = false
	}

	const customRequest = (data) => {
		console.warn("file=>", data)
		uploadLoading.value = true
		const fileData = new FormData()
		fileData.append('file', data.file)
		fileApi
			.fileUploadReturnId(fileData)
			.then((res) => {
				data.file.url = sysConfig.API_URL + '/dev/file/download?id=' + res
				data.file.id = res
				data.onSuccess()
			})
			.finally(() => {
				uploadLoading.value = false
			})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
